﻿using AutoMapper;
using Harmontronics.DataAccess.IOperate.Business;
using Harmontronics.DataAccess.Operate.Business;
using Harmontronics.IService;
using Harmontronics.IService.Business;
using Harmontronics.Model.Common;
using Harmontronics.Model.Dto;
using Harmontronics.Model.Dto.Model;
using Harmontronics.Model.Entitys;
using Harmontronics.Repository;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Microsoft.Extensions.Logging.EventSource.LoggingEventSource;

namespace Harmontronics.Service
{
    public class StationService : IStationService
    {
        private readonly IStationOperate _stationOperate;
        private readonly IPlcSettingOperate _plcSettingOperate;
        private readonly IProductionLineOperate _productionLineOperate;
        private readonly IMapper _mapper;
        private readonly IUnitOfWork _unitOfWork;
        public StationService(IUnitOfWork unitOfWork, IMapper mapper, IStationOperate stationOperate, IPlcSettingOperate plcSettingOperate, IProductionLineOperate productionLineOperate)
        {
            _mapper = mapper; 
            _unitOfWork = unitOfWork;
            _stationOperate = stationOperate;
            _plcSettingOperate = plcSettingOperate;
            _productionLineOperate = productionLineOperate;
        }
        public bool AddStation(Station station)
        {
            try
            {
                _stationOperate.AddStation(station);
                _unitOfWork.Commit();
            }
            catch (Exception ex)
            {
                return false;
            }
            return true;
        }

        public bool DeleteStationById(string id)
        {
            try
            {
                Station entity = _stationOperate.GetStationById(id);
                if (entity != null)
                {
                    _stationOperate.DeleteStation(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {

                return false;
            }
        }
        public StationModel Mending(Station entity)
        {
            StationModel model = _mapper.Map<StationModel>(entity);
            ProductionLine line = _productionLineOperate.GetProductionLineById(entity.ProductionLineId);
            if (line != null)
            {
                model.ProductionLine = line;
            }
            else
            {
                model.ProductionLine = new ProductionLine()
                {
                    ProductionLineId = entity.ProductionLineId,
                };
            }
            PlcSetting setting = _plcSettingOperate.GetPlcSettingById(entity.PlcId);
            if (setting != null)
            {
                model.PlcSetting = setting;
            }
            else
            {
                model.PlcSetting = new PlcSetting()
                {
                    PlcId = entity.PlcId
                };
            }
            return model;
        }
        public List<StationModel> MendingRange(List<Station> entities)
        {
            List<StationModel> models = new List<StationModel>();
            foreach (Station entity in entities)
            {
                models.Add(Mending(entity));
            }
            return models;
        }
        public List<StationModel> GetAllStationModels()
        {
            try
            {
                List<Station> entities = _stationOperate.GetAllStationList();
                List<StationModel> models = new List<StationModel>();
                foreach (var entity in entities)
                {
                    models.Add(Mending(entity));
                }
                return models;
            }
            catch (Exception ex)
            {
                return new List<StationModel>();
            }
        }
        public List<Station> GetAllStations()
        {
            try
            {
                List<Station> entities = _stationOperate.GetAllStationList();
                return entities;
            }
            catch (Exception ex)
            {
                return new List<Station>();
            }
        }
        public StationModel GetStationsById(string id)
        {
            try
            {
                Station entity = _stationOperate.GetStationById(id);
                return Mending(entity);
            }
            catch (Exception e)
            {
                return null;
            }
        }


        public List<StationModel> GetStationsByPlcId(string id)
        {
            try
            {
                List<Station> entities = _stationOperate.GetStationByPlcId(id);
                List<StationModel> models = new List<StationModel>();
                foreach (var entity in entities)
                {
                    models.Add(Mending(entity));
                }
                return models;
            }
            catch (Exception e)
            {
                return null;
            }
        }


        public bool UpdateStation(Station station)
        {
            try
            {
                Station entity = _stationOperate.GetStationById(station.WorkStationId);
                if (entity != null)
                {
                    _mapper.Map(station, entity);
                    _stationOperate.UpdateStation(entity);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        public bool BatchDelWorkStation(List<string> ids)
        {
            try
            {
                List<Station> entities = _stationOperate.GetStationByIdList(ids);
                if (entities.Count > 0)
                {
                    _stationOperate.BatchDelStation(entities);
                    _unitOfWork.Commit();
                }
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }

        }

    }
}
